<template>
  <div style="margin-top:30px;">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px">
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="名称" prop="name" style="margin-right:15px">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="助记符" prop="mnemonic" style="margin-right:15px">
              <el-input v-model="form.mnemonic" placeholder="请输入助记符" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="商品类型" prop="typeCode" style="margin-right:15px">
              <el-cascader ref="goodsType" v-model="type" :options="data" :show-all-levels="false" style="width: 100%" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="规格" prop="spec" style="margin-right:15px">
              <el-select v-model="form.spec" placeholder="请输入规格" clearable style="width:100%">
                <el-option
                  v-for="item in specData"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>

            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="条码"  style="margin-right:15px">
              <el-input v-model="form.barCode" placeholder="请输入条码" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="单位" prop="unitCode" style="margin-right:15px">
              <unitElSelect ref="unitName" style="width: 100%;" @queryUnitCode="selectUnitCode" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="辅助单位" prop="auxiliaryUnitCode" style="margin-right:15px">
              <auxiliaryUnitElSelect ref="auxiliaryUnitName" style="width: 100%;" @queryAuxiliaryUnitCode="selectAuxiliaryUnitName" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="换算率" prop="convertedQuantity" style="margin-right:15px">
               <el-input-number v-model="num" :min="0" :max="1000"  label="描述文字" @change="handleChange" :precision="4" :step="0.001" ></el-input-number>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="最低库存量预警" prop="miniStock" style="margin-right:15px">
              <el-input v-model="form.miniStock" placeholder="请输入最低库存量预警" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="最高库存量预警" prop="maxStock" style="margin-right:15px">
              <el-input v-model="form.maxStock" placeholder="请输入最高库存量预警" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="funcCancel">取 消</el-button>
      <el-button type="primary" @click="funcSubmit" :disabled="isDisable">确 定</el-button>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import request from '@/utils/request'
import unitElSelect from './unitElSelect.vue'
import auxiliaryUnitElSelect from './auxiliaryUnitElSelect.vue'

Vue.use(VXETable)
export default {
  name: 'GoodsEdit',
  components: {
    unitElSelect,
    auxiliaryUnitElSelect
  },
  data() {
    return {
      isDisable:false,
      specData:[],//规格数据
      emptyForm: {},
      data: [],
            num:'',

      form: {
        code: '',
        name: '',
        mnemonic: '',
        typeCode: '',
        spec: '',
        lotNumber: '',
        barCode: '',
        unitCode: '',
        auxiliaryUnitCode: '',
        miniStock: '',
        maxStock: '',
        typeCodes: [],
        convertedQuantity:'',
        type: []
      },
      type: [],
      unitCodeList: [],
      auxiliaryUnitCodeList: [],
      rules: {
        convertedQuantity: [
          { required: true, message: '请输入名称', trigger: 'blur' },
        ],

        code: [
          { required: true, message: '请输入编码', trigger: 'blur' },

        ],
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },

        ],
        mnemonic: [
          { required: true, message: '请输入助记符', trigger: 'blur' },

        ],
        typeCode: [
          { required: true, message: '请输入商品类型', trigger: 'blur' },

        ],
        spec: [
          { required: true, message: '请输入规格', trigger: 'blur' },

        ],
        lotNumber: [
          { required: true, message: '请输入批号', trigger: 'blur' },

        ],
        barCode: [
          { required: true, message: '请输入条码', trigger: 'blur' },

        ],
        unitCode: [
          { required: true, message: '请输入单位', trigger: 'blur' },

        ],
        auxiliaryUnitCode: [
          { required: true, message: '请输入辅助单位', trigger: 'blur' },

        ]
      }
    }
  },
  created() {
    // 保存空表单对象
    this.emptyForm = JSON.parse(JSON.stringify(this.form))
    // this.getSpec()

  },
  methods: {
    //获取规格
    getSpec(){
      request({ url: '/arpa-basic-api/dict/listDict?types=goodsSpec', method: 'get',}).then(response => {
        this.specData = response.data[0].dictList
      })
    },

    handleChange(value) {
        this.form.convertedQuantity = value
    },

    selectUnitCode(value) {
      this.form.unitCode = value
    },
    selectAuxiliaryUnitName(value) {
      this.form.auxiliaryUnitCode = value
    },
    // 获取页面信息
    getInfo(code, data, unitCodeList, auxiliaryUnitCodeList) {
      this.data = data
      this.unitCodeList = unitCodeList
      this.auxiliaryUnitCodeList = auxiliaryUnitCodeList
      // 查询到数据之前先展示空表单
      this.form = JSON.parse(JSON.stringify(this.emptyForm))
      request({ url: `/arpa-basic-api/goods/${code}`, method: 'get' }).then(response => {
        this.form = response.data
        this.$refs.unitName.setSelectValue(this.form.unitCode)
        this.$refs.auxiliaryUnitName.setSelectValue(this.form.auxiliaryUnitCode)
        this.type = this.form.typeCodes;
        this.num = this.form.convertedQuantity
      })
    },

    // 提交
    funcSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.isDisable = true

          const length = this.type.length
          if (length > 0) {
            this.form.typeCode = this.type[length - 1]
          }
          const data = this.form
          request({ url: '/arpa-basic-api/goods', method: 'put', params: data }).then(response => {
            this.$message({ message: response.msg, type: 'success' })
            this.$emit('fatherEditMethod')
            this.isDisable = false

            }).catch(() => this.isDisable = false)
        } else {
          return false
        }
      })
    },
    // 取消
    funcCancel() {
      this.$emit('fatherCancelMethod', 'edit')
    }

  }
}
</script>
<style lang="scss" scoped>
    .dialog-footer {
        margin:15px 0 15px 0;
        width: 175px;
        float: right;
    }
</style>
